package com.miku.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miku.blog.domain.LikeCount;
import com.miku.blog.domain.pojo.ResponseResult;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.service.LikeCountService;
import com.miku.blog.mapper.LikeCountMapper;
import com.miku.blog.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Miku
 * @description 针对表【tb_like_count】的数据库操作Service实现
 * @createDate 2022-06-14 22:38:06
 */
@Service
public class LikeCountServiceImpl extends ServiceImpl<LikeCountMapper, LikeCount>
        implements LikeCountService {


    @Override
    public List<LikeCount> getLikeCountByMostLikedArticles(Integer articleId) {
        Page<LikeCount> page = new Page<>(SystemConstance.PAGE_CURRENT_MIN, SystemConstance.RECOMMEND_ARTICLE_SIZE);
        QueryWrapper<LikeCount> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("target_id,type,Count(*) AS count")
                .eq("type", SystemConstance.LIKE_COUNT_TYPE_BY_ARTICLE)
                .ne("target_id", articleId)
                .groupBy("target_id")
                .orderByDesc("count");
        page(page, queryWrapper);
        List<LikeCount> records = page.getRecords();
        return records;
    }


    @Override
    public List<Integer> getLikeCountListByUserIdAndType(Integer userId,Integer type) {
        LambdaQueryWrapper<LikeCount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LikeCount::getUserId, userId)
                .eq(LikeCount::getType, type);

        List<LikeCount> likeCountList = list(queryWrapper);

        return likeCountList.stream()
                .map(LikeCount::getTargetId)
                .collect(Collectors.toList());
    }


    @Override
    public long getLikeCountByTargetIdAndType(Integer targetId,Integer type) {
        LambdaQueryWrapper<LikeCount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LikeCount::getTargetId, targetId)
                .eq(LikeCount::getType, type);
        return count(queryWrapper);
    }


    @Override
    public ResponseResult addLikeCountByIdAndIsLikeAndType(Integer targetId, Integer isLike,Integer type) {

        if (SystemConstance.IS_LIKE.equals(isLike)) {
            LambdaQueryWrapper<LikeCount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(LikeCount::getUserId, SecurityUtils.getUserAuth().getId())
                    .eq(LikeCount::getTargetId, targetId)
                    .eq(LikeCount::getType, type);

            if (!remove(queryWrapper)) {
                return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
            }

        } else if (SystemConstance.NOT_LIKE.equals(isLike)) {

            LikeCount likeCount = new LikeCount();
            Integer userId = SecurityUtils.getUserAuth().getId();
            likeCount.setUserId(userId)
                    .setTargetId(targetId)
                    .setType(type);
            if (!save(likeCount)) {
                return new ResponseResult(HttpStatusEnum.INSERT_DATA_ERROR);
            }
        }else {
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }

        return ResponseResult.ok();
    }


    @Override
    public boolean delLikeCountByTargetIdAndType(Integer targetId, Integer type) {


        LambdaQueryWrapper<LikeCount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LikeCount::getTargetId,targetId)
                .eq(LikeCount::getType,type);

        return remove(queryWrapper);
    }
}




